'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 商品列表
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var async = require('async');
var resourceConf = require('../../../../config/resourceConf');
var dataApi = require('../../../../libs/dataApi');
var vipShopConf = require('../../../../config/vipShopConf');
var PlayerManager = require('../../../../libs/playerManager');
var Code = require('../../../../consts/code');

module.exports = function (msg, session, next) {
    var role_id = session.uid;

    var shop_list = [];
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.getModel(['role', 'shop', 'vip', 'accrued'], cb);
            },
            function (cb) {
                var role_model = player_manager.role;
                var shop_model = player_manager.shop;
                dataApi.resource_shop.each(function (value, shop_type) {
                    var shop_item = _.clone(value, true);
                    shop_item.total_buy_num = vipShopConf.getVipBuyShopNum(shop_type, role_model.getVip()) + value.time_limit;
                    shop_item.can_buy_num = shop_item.total_buy_num - shop_model.getBuyResourceShopNum(shop_type);
                    shop_list.push(shop_item);
                });

                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }

            return next(null, {
                code: Code.OK,
                result: {
                    shop_list: shop_list
                }
            });
        }
    );
};
